/* 打开一个标签 */
function OpenTab(tabid, title, url){
    /**
    如果这个标题的标签存在，则选择该标签
    否则添加一个标签到标签组
    */
    if(tabid.tabs('exists', title)){
        tabid.tabs('select', title);
    }else{
        if(tabid.find('.tabs li').length>10){
            $.messager.alert("警告","打开的标签过多，请关闭其他标签后再试！");
        }else{
            tabid.tabs('add',{
                title: title,
                // content: CreateTabContent(url),
                href: url,
                closable: true,
                fit: true,
                height: 'auto'
            });

        }
    }
}

/* 生成标签内容 */
function CreateTabContent(url){
    return '<iframe style="width:100%;height:100%;" scrolling="auto" frameborder="0" src="' + url + '"></iframe>';
}

$(function(){

    $('#dzTree').tree({
        url:'/dz_tree',
        method:'get',
        lines: true,
        toggleOnClick: true,
        onClick: function(node){
            console.log(node.attributes);
            //console.log(node.attributes.url);
            if(node.attributes!=undefined){
                console.log(node.attributes.url);  // alert node text property when clicked
                var tabid=$("#mainTabs");
                var title=node.text;
                var url=node.attributes.url;
                // var icon=$(this).attr("icon");
                OpenTab(tabid,title,url);
            
            }
        }
    });
/*

    $('#glTree').tree({
        url:'/gl_tree',
        method:'get',
        lines: true,
        toggleOnClick: true,
        onClick: function(node){
            console.log(node.attributes);
            //console.log(node.attributes.url);
            if(node.attributes!=undefined){
                console.log(node.attributes.url);  // alert node text property when clicked
                var tabid=$("#mainTabs");
                var title=node.text;
                var url=node.attributes.url;
                // var icon=$(this).attr("icon");
                OpenTab(tabid,title,url);
            
            }
        }
    });
*/
    $("#mainMenu").accordion({
        onSelect: function(title,index){
            switch(index){
/*
                case 0:
                    $('#dzTree').tree({
                        url:'/dz_tree',
                        method:'get',
                        lines: true,
                        toggleOnClick: true,
                        onClick: function(node){
                            console.log(node.attributes);
                            //console.log(node.attributes.url);
                            if(node.attributes!=undefined){
                                console.log(node.attributes.url);  // alert node text property when clicked
                                var tabid=$("#mainTabs");
                                var title=node.text;
                                var url=node.attributes.url;
                                // var icon=$(this).attr("icon");
                                OpenTab(tabid,title,url);
                            
                            }
                        }
                    });
                    break;
*/
                case 1:
                    var glt=$('#glTree').children().length;
                    if(!glt){
                        $('#glTree').tree({
                            url:'/gl_tree',
                            method:'get',
                            lines: true,
                            toggleOnClick: true,
                            onClick: function(node){
                                console.log(node.attributes);
                                //console.log(node.attributes.url);
                                if(node.attributes!=undefined){
                                    console.log(node.attributes.url);  // alert node text property when clicked
                                    var tabid=$("#mainTabs");
                                    var title=node.text;
                                    var url=node.attributes.url;
                                    // var icon=$(this).attr("icon");
                                    OpenTab(tabid,title,url);
                                
                                }
                            }
                        });
                    }
                    break;
            }
        }
    });
});